// 1.姓名脱敏
export function desensitizationName(str) {
	if (null != str && str != undefined) {
		/* if (str.length == 2) {
			return str.substring(0, 1) + '*' //截取name 字符串截取第一个字符，
		} else if (str.length == 3) {
			return str.substring(0, 1) + "*" + str.substring(2, 3) //截取第一个和第三个字符
		} else if (str.length > 3) {
			return str.substring(0, 1) + "*" + '*' + str.substring(3, str.length) //截取第一个和大于第4个字符
		} */
		if (null != str && str != undefined) {
			return new Array(str.length).join('*') + str.substring(str.length - 1, str.length)
		}
	} else {
		return "";
	}
}
// 2.手机号脱敏
export function desensitizationPhone(str) {
	if (null != str && str != undefined) {
		// var pat = /(\d{3})\d*(\d{4})/;
		var pat = /(\d{3})\d*(\d{2})/;
		return str.replace(pat, "$1****$2");
	} else {
		return "";
	}
}
// 3.身份证号脱敏
export function desensitizationIdCard(str) {
	if (null != str && str != undefined) {
		// var pat = /(\d{6})\d*(\d{4})/;
		var pat = /(\d{1})\d*(\d{1})/;
		return str.replace(pat, "$1***********$2");
	} else {
		return "";
	}
}
// 4.邮箱脱敏
export function desensitizationEmail(email) {
	let new_email = email;
	if (String(email).indexOf("@") > 0) {
		let str = email.split("@");
		let _s = "";
		if (str[0].length > 3) {
			for (let i = 3; i < str[0].length; i++) {
				_s += "*";
			}
			new_email = str[0].substr(0, 3) + _s + "@" + str[1];
		} else {
			for (let i = 1; i < str[0].length; i++) {
				_s += "*";
			}
			new_email = str[0].substr(0, 1) + _s + "@" + str[1];
		}
	}
	return new_email;
}
// 5.字符串托敏
export function desensitizationNormalStr(str, headLen = 1, tailLen = 1, maskChar = '*') {
	if (!str || str.length <= headLen + tailLen) {
		return str || '';
	}
	const maskedPartLength = str.length - headLen - tailLen;
	let maskedPart = new Array(maskedPartLength + 1).join(maskChar);
	return str.slice(0, headLen) + maskedPart + str.slice(-tailLen);
}
// 日期格式化
export function parseTime(time, pattern) {
	if (arguments.length === 0 || !time) {
		return null
	}
	const format = pattern || '{y}-{m}-{d} {h}:{i}:{s}'
	let date
	if (typeof time === 'object') {
		date = time
	} else {
		if ((typeof time === 'string') && (/^[0-9]+$/.test(time))) {
			time = parseInt(time)
		} else if (typeof time === 'string') {
			time = time.replace(new RegExp(/-/gm), '/');
		}
		if ((typeof time === 'number') && (time.toString().length === 10)) {
			time = time * 1000
		}
		date = new Date(time)
	}
	const formatObj = {
		y: date.getFullYear(),
		m: date.getMonth() + 1,
		d: date.getDate(),
		h: date.getHours(),
		i: date.getMinutes(),
		s: date.getSeconds(),
		a: date.getDay()
	}
	const time_str = format.replace(/{(y|m|d|h|i|s|a)+}/g, (result, key) => {
		let value = formatObj[key]
		// Note: getDay() returns 0 on Sunday
		if (key === 'a') {
			return ['日', '一', '二', '三', '四', '五', '六'][value]
		}
		if (result.length > 0 && value < 10) {
			value = '0' + value
		}
		return value || 0
	})
	return time_str
}